package gr.atc.epart;

import gr.atc.epart.CustomCategoriesManager.CustomTabData;
import gr.atc.epart.actions.Action.Type;
import gr.atc.epart.net.ActionsPage;
import gr.atc.epart.net.ActionsPage.SortBy;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TabHost;

public class EPartDesktopActivity extends TabActivity // implements
// TabContentFactory
{

	EPartClient client = new EPartClient();
	TabHost tabHost = null;
	String username;

	private static final int CREATE_ACTION_ID = 0;
	private static final int SETTINGS_ID = 1;
	private static final int ADD_CATEGORY_ID = 2;
	private static final int ADD_CATEGORY_CODE = 4;
	private static final int CREATE_ACTION_CODE = 3;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		username = AccountUtil.getAccountName(this.getBaseContext());
		tabHost = getTabHost();
		tabHost.getTabWidget().setHorizontalScrollBarEnabled(true);

		tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec("Popular").setIndicator("Popular",
				getResources().getDrawable(android.R.drawable.star_big_off))
				.setContent(new Intent(this, ActionsViewActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("Mine")
				.setIndicator(
						"Mine",
						getResources().getDrawable(
								android.R.drawable.ic_menu_myplaces))
				.setContent(new Intent(this, ActionsViewActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("Search").setIndicator("Search",
				getResources().getDrawable(android.R.drawable.ic_menu_search))
				.setContent(new Intent(this, SearchActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("Maps").setIndicator("Maps",
				getResources().getDrawable(android.R.drawable.ic_menu_mapmode))
				.setContent(new Intent(this, EPartMaps.class)));

		addCategories();

	}

	public static void retrieveActions(String _nameString, String _tagsString,
			String _creatorString, String _fromString, String _toString,
			String _sortByString, String _typeString, String tabTag) {

		SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

		Date _startDate = null;
		Date _endDate = null;

		try {
			if (_fromString != null && !_fromString.equals("")) {
				_startDate = formatter.parse(_fromString);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			if (_toString != null && !_toString.equals("")) {
				_endDate = formatter.parse(_toString);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Type type = Type.valueOf(_typeString);
		String[] _tags;
		if (_tagsString != null) {
			_tags = _tagsString.split(" ");
		} else {
			_tags = null;
		}

		EPartClient client = new EPartClient();

		ActionsPage actions = client.getActions(type,null,_creatorString,
				_startDate, _endDate, _tags, SortBy.Popularity, 1, 100);

		if (actions != null) {
			EPartVolatileDataStore.getInstance().setActionsList(tabTag,
					actions.getActions());
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, CREATE_ACTION_ID, Menu.NONE, "Create Action");
		menu.add(Menu.NONE, ADD_CATEGORY_ID, Menu.NONE, "Categories");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case CREATE_ACTION_ID:
			Intent actionDetailsIntent = new Intent();
			actionDetailsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.CreateActionActivity");
			startActivityForResult(actionDetailsIntent, CREATE_ACTION_CODE);
			return true;

		case SETTINGS_ID:
			Intent settingsIntent = new Intent();
			settingsIntent
					.setClassName("gr.atc.epart", "gr.atc.epart.Settings");
			startActivity(settingsIntent);
			return true;

		case ADD_CATEGORY_ID:

			tabHost.setCurrentTab(0);
			Intent addCategoryIntent = new Intent();
			addCategoryIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.CategoryMgrActivity");
			startActivityForResult(addCategoryIntent, ADD_CATEGORY_CODE);
			return true;

		default:
			return false;
		}

	}

	private void updateTabs() {

		try
		{
		tabHost = getTabHost();
			tabHost.setCurrentTab(0);
		tabHost.clearAllTabs();

			retrieveActions(null, null, "all", null, null, "Popularity", "All","Popular");

		tabHost.addTab(tabHost.newTabSpec("Popular").setIndicator("Popular",
				getResources().getDrawable(android.R.drawable.star_big_off))
				.setContent(new Intent(this, ActionsViewActivity.class)));

		retrieveActions(null, null, username, null, null, "Popularity", "All",
				"Mine");

		tabHost.addTab(tabHost.newTabSpec("Mine")
				.setIndicator(
						"Mine",
						getResources().getDrawable(
								android.R.drawable.ic_menu_myplaces))
				.setContent(new Intent(this, ActionsViewActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("Search").setIndicator("Search",
				getResources().getDrawable(android.R.drawable.ic_menu_search))
				.setContent(new Intent(this, SearchActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("Maps").setIndicator("Maps",
				getResources().getDrawable(android.R.drawable.ic_menu_mapmode))
				.setContent(new Intent(this, EPartMaps.class)));

		addCategories();
		}
		catch(Exception e)
		{
			Log.i("tabs", e.getMessage());
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {

		case 200:
			updateTabs();
			break;

		default:
			break;
		}
	}

	protected void addCategories() {

		CustomCategoriesManager categoryManager = new CustomCategoriesManager(
				this);
		int numberOfCategories = categoryManager.getCustomTabsNumber();

		for (int i = 0; i < numberOfCategories; i++) {
			CustomTabData tabData = categoryManager.getCustomTabData(i);
			ActionsPage actions = client.getActions(tabData.getActionType(),null,
					tabData.getCreator(), tabData.getStartDate(), tabData
							.getStopDate(), tabData.getTags(), tabData
							.getSortBy(), tabData.getPageNumber(), tabData
							.getPageSize());

			if (actions != null) {
				EPartVolatileDataStore.getInstance().setActionsList(
						tabData.getTabName(), actions.getActions());

				tabHost = getTabHost();

				tabHost
						.addTab(tabHost
								.newTabSpec(tabData.getTabName())
								.setIndicator(
										tabData.getTabName(),
										getResources()
												.getDrawable(
														android.R.drawable.ic_menu_view))
								.setContent(
										new Intent(this,
												ActionsViewActivity.class)));

			}
		}

	}
}
